﻿using System;
using System.Reflection;

namespace Fast.Extensions
{
    /// <summary>
    /// Type 扩展类
    /// </summary>
    [SkipScan]
    public static class FastTypeExtensions
    {
        /// <summary>
        /// 判断一个类型是否为基元类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsPrimitive(this Type type)
        {
            if (IsPrimitiveType(type))
            {
                return true;
            }

            if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                return IsPrimitiveType(type.GenericTypeArguments[0]);
            }

            bool IsPrimitiveType(Type type)
            {
                if (type.GetTypeInfo().IsPrimitive)
                {
                    return true;
                }

                return type == typeof(string) ||
                       type == typeof(decimal) ||
                       type == typeof(DateTime) ||
                       type == typeof(DateTimeOffset) ||
                       type == typeof(TimeSpan) ||
                       type == typeof(Guid);
            }

            return false;
        }


    }
}
